/*
 * @author Robert Hanwell
 * @version 3
 * @since January
 */
package com.smartbox.floodaction;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

// TODO: Auto-generated Javadoc
/**
 * The Class TaskMap.
 */
public class TaskMap extends MapActivity {

	/** The map. */
	private MapView map;

	/** The stop. */
	private long start, stop;

	/** The compass. */
	private MyLocationOverlay compass;

	/** The controller. */
	private MapController controller;

	/** The y. */
	int x, y;

	/** The touched point. */
	private GeoPoint touchedPoint;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.android.maps.MapActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		map = (MapView) findViewById(R.id.mapview);
		map.setBuiltInZoomControls(true);

		Touch t = new Touch();
		List<Overlay> overlaylist = map.getOverlays();
		overlaylist.add(t);
		compass = new MyLocationOverlay(TaskMap.this, map);
		overlaylist.add(compass);
		controller = map.getController();

		GeoPoint point = new GeoPoint(51, -2);
		controller.animateTo(point);
		controller.setZoom(11);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.android.maps.MapActivity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		compass.disableCompass();
		super.onPause();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.android.maps.MapActivity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		compass.enableCompass();
		super.onResume();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.android.maps.MapActivity#isRouteDisplayed()
	 */
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * The Class Touch.
	 */
	class Touch extends Overlay {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.google.android.maps.Overlay#onTouchEvent(android.view.MotionEvent
		 * , com.google.android.maps.MapView)
		 */
		@Override
		public boolean onTouchEvent(MotionEvent e, MapView m) {
			if (e.getAction() == MotionEvent.ACTION_DOWN) {
				start = e.getEventTime();
				x = (int) e.getX();
				y = (int) e.getY();
				touchedPoint = map.getProjection().fromPixels(x, y);
			}
			if (e.getAction() == MotionEvent.ACTION_UP) {
				stop = e.getEventTime();
			}
			// if stop is greater and 1500 length of press
			if (stop - start > 1500) {
				AlertDialog alert = new AlertDialog.Builder(TaskMap.this)
						.create();
				alert.setTitle("the location is");
				alert.setMessage("I told to pick option");
				alert.setButton("Place Pin",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						});
				alert.setButton2("Get Address",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								Geocoder geocoder = new Geocoder(
										getBaseContext(), Locale.getDefault());
								try {
									List<Address> address = geocoder.getFromLocation(
											touchedPoint.getLatitudeE6() / 1E6,
											touchedPoint.getLongitudeE6() / 1E6,
											1);
									String display = "";
									if (address.size() > 0) {
										for (int i = 0; i < address.get(0)
												.getMaxAddressLineIndex(); i++) {

											display += address.get(0)
													.getAddressLine(i) + "\n";

										}
										Toast t = Toast.makeText(
												getBaseContext(), display,
												Toast.LENGTH_LONG);
										t.show();
									}
								} catch (IOException e) {
									// TODO Auto-generated catch block
									Dialog d = new Dialog(TaskMap.this);
									String error = e.toString();
									TextView tv = new TextView(TaskMap.this);
									tv.setText(error);
									d.setContentView(tv);
									d.show();
									e.printStackTrace();
								} finally {

								}
							}
						});
				alert.setButton3("Option ",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						});
				alert.show();
				return true;

				/*
				 * GeoPoint c = m.getMapCenter(); c.getLatitudeE6();
				 * c.getLongitudeE6();
				 */

			}
			return false;
		}
	}

}
